package com.itbaizhan.controller;

import com.itbaizhan.pojo.User;
import com.itbaizhan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/*用户控制层
* */
//@Controller
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;




    /*添加用户*/
    @PostMapping("/addUser")
    public String adduser(User user) {
        userService.addUser(user);
        return "redirect:/ok";
    }

    @GetMapping("/getUser")
    public String findUser(Model model) {
        List<User> users = userService.findAll();
        //把数据放进model中，在前端遍历
        model.addAttribute("users", users);
        return "showuser";
    }

    //更新用户的第一步，根据用户id查到用户
    @GetMapping("/findByid")
    public String findByid(Long id,Model model) {
        User user = userService.findById(id);
        model.addAttribute("user",user);
        return "updateuser";
    }

    @PostMapping("/updateUser")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/ok";
    }
    @GetMapping("/deleteUserById")
    public String deleteUserByid(Long id){
        userService.deleteUserById(id);
        return "redirect:/ok";
    }

}
